package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.*;
import com.suyuan.entity.*;
import com.suyuan.service.StevedoringService;
import com.suyuan.vo.NewTransitBatchVO;
import com.suyuan.vo.StevedoringVO;
import com.suyuan.vo.TransitBatchInfoVO;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang on 2018/2/17.
 */
@Service(value = "stevedoringService")
public class StevedoringServiceImpl implements StevedoringService {

    @Resource
    private StevedoringInforMapper stevedoringMapper;
    @Resource
    private ProductInfoMapper productInfoMapper;
    //@Resource
    //private TransitbatchInfoMapper transitBatchInfoMapper;
    @Resource
    private NewtransitbatchMapper newtransitbatchMapper;
    @Resource
    private TransportrouteInforMapper routeMapper;
    @Resource
    private CompanyInfoMapper companyMapper;
    @Resource
    private CarinfoMapper carinfoMapper;

    public PageInfo list(Integer stevedoringId, Integer productId, Page page) {
        StevedoringInforExample example = new StevedoringInforExample();
        StevedoringInforExample.Criteria criteria = example.createCriteria();
        if(stevedoringId != null){
            criteria.andStevedoringidEqualTo(stevedoringId);
        }
        if(productId != null){
            criteria.andProductidEqualTo(productId);
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<StevedoringInfor> stevedorings = stevedoringMapper.selectByExample(example);
        PageInfo info = new PageInfo(stevedorings);

        List<StevedoringVO> vos = new ArrayList<StevedoringVO>();
        for(StevedoringInfor steve : (List<StevedoringInfor>)info.getList()){
            //批次
            Newtransitbatch batch = newtransitbatchMapper.selectByPrimaryKey(steve.getTransitBatchId());
            //路线
            TransportRouteInfor route = routeMapper.selectByPrimaryKey(batch.getRouteid());
            //公司信息
            CompanyInfo company = companyMapper.selectByPrimaryKey(batch.getCompid());
            //车辆信息
            CarInfo car = carinfoMapper.selectByPrimaryKey(batch.getCarid());

            //构造批次的vo类
            NewTransitBatchVO batchVO = new NewTransitBatchVO(batch,route,company,car);

            //商品
            ProductInfo product = productInfoMapper.selectByPrimaryKey(steve.getProductId());
            vos.add(new StevedoringVO(steve,product,batchVO));
        }
        info.setList(vos);
        return info;
    }

    public int save(StevedoringInfor stevedoring) {
        return stevedoringMapper.insert(stevedoring);
    }

    public int delete(Integer id) {
        return stevedoringMapper.deleteByPrimaryKey(id);
    }

    public List<ProductInfo> getProductList() {
        List<ProductInfo> infos = productInfoMapper.selectByExample(new ProductInfoExample());
        return infos;
    }

    public StevedoringInfor findById(Integer id) {
        return stevedoringMapper.selectByPrimaryKey(id);
    }

    public int update(StevedoringInfor stevedoring) {
        return stevedoringMapper.updateByPrimaryKey(stevedoring);
    }
}
